<?php

namespace App\Controllers\Order;

use App\Configs\Web\Pay;
use App\Models\Order\Order;
use \App\Libs\Pay\PayLibs;

class AlipayGetOrderStatus extends \App\Libs\Base\BaseAction
{

    protected function beforeRun()
    {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }

    public function execute()
    {
        if (!$this->request->has('order_sn')) {
            return \App\Libs\Views\Json::view(1, '参数不完整', NULL);
        }
//        $uid =$this->request->get('uid');
        $uid = $this->session->user->id;
        $order_sn = $this->request->get('order_sn');
        $matchOrder = Order::find([
            'conditions' => 'uid=?1 and order_sn=?2',
            'bind' => [
                1 => $uid,
                2 => $order_sn
            ]
        ]);
        if (empty($matchOrder)) {
            return \App\Libs\Views\Json::view(1, '订单号不存在!', null);
        }
//        是否已支付 0:待支付  1:已支付 2:已取消 3.已过期 4.已失效 5.已使用 6.已退款 9.异常
        switch ($matchOrder->status) {
            case '1' :
                //本地库已支付，返回支付成功;
                $data = [
                    'order_sn' => $order_sn,
                    'status_text' => $this->getText($matchOrder->status),
                    'status' => '1'
                ];
                return \App\Libs\Views\Json::view(0, '订单支付成功', $data);
                break;
            case '0':
                //本地库未更新，请等待结果返回
                //todo::更换形式,
                sleep(3);
                $secondQueryObj = Order::find([
                    'conditions' => 'uid=?1 and order_sn=?2',
                    'bind' => [
                        1 => $uid,
                        2 => $order_sn
                    ]
                ]);
                if ($secondQueryObj->status == '1') {
                    //支付结果已经返回
                    $data = [
                        'order_sn' => $order_sn,
                        'status_text' => $this->getText($secondQueryObj->status),
                        'status' => '1'
                    ];
                    return \App\Libs\Views\Json::view(0, '支付成功', $data);
                } elseif ($secondQueryObj->status == '0') {
                    //支付结果返回中
                    $data = [
                        'order_sn' => $order_sn,
                        'status_text' => $this->getText($secondQueryObj->status),
                        'status' => '0'
                    ];
                    return \App\Libs\Views\Json::view(1, '支付结果返回中，请稍后查看!', $data);
                } else {
                    $data = [
                        'order_sn' => $order_sn,
                        'status_text' => $this->getText($secondQueryObj->status),
                        'status' => $matchOrder->status
                    ];
                    return \App\Libs\Views\Json::view(1, '接口异常调用,返回此订单状态标识', $data);
                }
                break;
            default:
                $data = [
                    'order_sn' => $order_sn,
                    'status_text' => $this->getText($matchOrder->status),
                    'status' => $matchOrder->status
                ];
                return \App\Libs\Views\Json::view(1, '接口异常调用,返回此订单状态标识', $data);
                break;
        }
    }

    public function getText($status)
    {
        switch ($status) {
//        0:待支付  1:已支付 2:已取消 3.已过期 4.已失效 5.已使用 6.已退款 9.异常
            case '0':
                $text = '待支付';
                break;
            case '1':
                $text = '已支付';
                break;
            case '2':
                $text = '已取消';
                break;
            case '3':
                $text = '已过期';
                break;
            case '4':
                $text = '已失效';
                break;
            case '5':
                $text = '已使用';
                break;
            case '6':
                $text = '已退款';
                break;
            case '9':
                $text = '订单异常';
                break;
        }
        return '订单状态：' . $text;
    }

}
